import * as ConstantData from '../../common/constant_data.js';
import * as commonUtil from '../../common/common.js';
var app = getApp()
Page({
  data: {
    checked: true,
    showTip: false
  },
  onLoad(options) {
    console.log('lllll2', app.globalData.wxScan.qrc)
    this.setData({
      checked: app.globalData.defCheckAgreeService
    })
  },

  clickShowTip() {
    const vm = this

    if (vm.data.showTip) {
      return;
    }

    vm.setData({
      showTip: true
    })

    // setTimeout(() => {
    //   vm.setData({
    //     showTip: false
    //   })
    // }, 3000)
  },
  onclose() {
    this.setData({
      showTip: false
    })
  },
  setChecked() {
    const vm = this
    vm.setData({
      checked: true
    })
  },

  changeChecked() {
    const vm = this
    vm.setData({
      checked: !vm.data.checked
    })
  },

  checkboxChange: function (e) {
    this.data.checkedVal = e.detail.value
    this.setData({
      checked: !this.data.checked
    })
  },

  lookReadme() {
    wx.navigateTo({
      url: '../../pkgBak/pages/help/lease/lease',
    })
  },



  /**
   * 获取用户手机号
   */
  getPhoneNumber: function (e) {
    var that = this;

    commonUtil.recordErrorInfo(ConstantData.err_type.log_click_login_btn, '点击登录按钮:' + e.detail.errMsg);

    if (e.detail.errMsg.indexOf('user deny') != -1) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    } else {
      wx.showLoading({
        title: '登录中...',
      });
      //解密数据并登录成功
      app.executeRequest(function (header) {

        wx.request({
          url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease/auth/wxPhoneAuthLogin',
          method: 'post',
          header: header,
          data: {
            iv: encodeURIComponent(e.detail.iv),
            encryptedData: encodeURIComponent(e.detail.encryptedData)
          },
          success: function (res) {
            getApp().hideLoading();
            console.log('getPhoneNumber--res', res);
            let netErr = app.handleRes(res, that);
            if (netErr) {
              wx.navigateTo({
                url: '/pages/login/login',
              })
              return
            }
            if (res.data.result === 0) {
              //登录成功
              wx.showToast({
                title: '登录成功',
                icon: 'none'
              })
              var btt = that.data.phoneAuthBtt;
              wx.setStorageSync('nophone', '2');
              wx.setStorageSync('iphone', res.data.phone)
              app.globalData.loginData.nophone = 2;
              that.setData({
                user_id: res.data.uid ? res.data.uid : '',
                phoneAuthStatus: false,
                phoneAuthBtt: ''
              })
              console.log('btt------->', btt)
              app.globalData.loginFlag=1
              setTimeout(() => {
                let nophone = app.getUserLoginNoPhone()
                let qrc = app.globalData.wxScan.qrc;
                console.log(777777, nophone, qrc)
                if (btt == 'scan') {
                  if (qrc) {
                    console.warn('登录成功直接连接');
                    that.getDevIdAndConnect(qrc, 'login');
                  } else {
                    console.warn('登录成功调起摄像头');
                    that.scan();
                  }
                }else if (btt == 'record_user_deposit') {
                  wx.navigateTo({
                    url: '../user_record/deposit/user_deposit',
                  })
                } else if (btt == 'outpatient_add_employer') {
                  //爱心租赁
                  wx.navigateTo({
                    url: '../outpatient_service/manage_employer/manage_employer',
                  })
                } else if (btt == 'outpatient_manage_careworker') {
                  //门诊爱心租赁护工管理
                  wx.navigateTo({
                    url: '../outpatient_service/manage_careworker/manage_careworker',
                  })
                } else if (btt == 'deposit_record') {
                  //押金记录
                  wx.navigateTo({
                    url: '../user_record/deposit/user_deposit',
                  })
                } else if (btt == 'no_return_blue_timeout') {
                  //押金记录
                  let desc = wx.getStorageSync("alertBoxTipContent");
                  that.setData({
                    alertBoxStatus: true,
                    alertBoxTipContent: desc,
                    alertBoxBtnName: "确 定"
                  })
                } else if (btt == 'manager_hospitallist') {
                  wx.navigateTo({
                    url: '../../pkgBak/pages/manager/hospitallist/hospitallist',
                  })
                } else if (btt == 'opt_user_admin_menus') {
                  wx.navigateTo({
                    url: '../opt_user_admin/menus/menus',
                  })
                } else if (btt == 'user_phone_code') {
                  wx.navigateTo({
                    url: '../../pkgBak/pages/help/user_phone_coce/user_phone_code',
                  })
                } else {
                  // wx.reLaunch({
                  //   url: '../index/index',
                  // })
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }, 1000)
            } else if (res.data.result === 4) {
              wx.showToast({
                title: res.data.description,
                icon: "none"
              });
            } else {
              that.setData({
                phoneAuthStatus: true,
                phoneAuthBtt: 'scan',
                loading_show: false
              });
              wx.showToast({
                title: '请先登录',
                icon: 'none'
              })
            }
          },
          fail: function (err) {
            getApp().hideLoading();
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        });

      });
    }
  },

  toPhoneLogin: function () {
    wx.navigateTo({
      url: '../login/login',
    })
  },


});